<?php

namespace app\order\logic;

use Throwable;
use app\order\model\OrderRefundModel;
use think\facade\Db;

class OrderRefundLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new OrderRefundModel();
    }

    /**
     * @desc  列表
     * @date  2024/3/21 0021
     * @param $params
     * @return \think\response\Json
     * @author ligang
     */
    public function getOrderRefundList($params = [])
    {

        $orderId          = $params['order_id'] ?? '';
        $cancelReasonText = $params['cancel_reason_text'] ?? '';
        $cancel_status    = $params['cancel_status'] ?? '';
        $limit            = $params['limit'] ?? 10;

        $where            = [];
        $order            = ['create_time' => 'desc', 'id' => 'desc'];

        if ($orderId) {
            $order_ids = trim($orderId);
            $order_ids = array_filter(array_map('trim', explode("\n", $order_ids)));
            if (!empty($order_ids)) {
                $where[] = ['order_id', 'in', $order_ids];
            }
        }

        if ($cancelReasonText) {
            $where[] = ['cancel_reason_text', 'like', '%' . $cancelReasonText . '%'];
        }

        if ($cancel_status) {
            $where[] = ['cancel_status', '=', $cancel_status];
        }

        $res =$this->model
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items();
        $list = objectToArray($list);

        $shop_ids  = array_column($list,'shop_id');
        $shop_ids  = array_values(array_unique($shop_ids));

        $order_ids = array_column($list,'order_id');
        $order_ids = array_values(array_unique($order_ids));


        //批量获取店铺信息及sku数量信息;
        $shopInfos     = Db::name('shops')->whereIn('id',$shop_ids)->field('name,id')->select();
        $shopInfos     = objectToArray($shopInfos);
        $shopInfos_map = array_column($shopInfos,null,'id');

        //批量获取sku数量数据;
        $skuInfos     = Db::name('order_info')->whereIn('order_id',$order_ids)->field('seller_sku,order_id')->select();
        $skuInfos     = objectToArray($skuInfos);
        $skuInfos_map = self::get_sku_info($skuInfos);
        if($list){
            foreach($list as $k=>$v){
                $shop_names                = $shopInfos_map[$v['shop_id']]??[];
                $sku_names                 = $skuInfos_map[$v['order_id']]??[];
                $list[$k]['shop_name_arr'] = $shop_names['name']??'';
                $list[$k]['skus_info']     = $sku_names ? implode(',',$sku_names):'';
            }
        }
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }




    /**
     * @desc  修改为已处理
     * @date  2024/4/09
     * @author luckyXiong
     */
    public function processed($params=[]){
        $ids   = $params['ids'];
        $ids   = is_array($ids) ? $ids : explode(',', $ids);
        //批量更新;
        $this->model->whereIn('id', $ids)
            ->update(
                ['cancel' => 2]
            );
        return json(DefaultResponse(200, true, '操作成功!'));
    }




    /**
     * @desc  修改为未处理
     * @date  2024/4/09
     * @author luckyXiong
     */
    public function unprocessed($params=[]){
        $ids   = $params['ids'];
        $ids   = is_array($ids) ? $ids : explode(',', $ids);
        //批量更新;
        $this->model->whereIn('id', $ids)
            ->update(
                ['cancel' => 1]
            );
        return json(DefaultResponse(200, true, '操作成功!'));
    }







    /**
     * @desc获取sku信息
     * @date  2024-03-28
     * @author  luckyXiong
     */
    public function get_sku_info($originalArray=[]){
        $newArray = [];
        foreach ($originalArray as $item) {
            $orderId = $item['order_id'];
            unset($item['order_id']);

            if (!isset($newArray[$orderId])) {
                $newArray[$orderId] = [];
            }

            $newArray[$orderId][] = $item['seller_sku'];
        }
        foreach ($newArray as &$values) {
            $values = array_combine(range(0, count($values) - 1), array_values($values));
        }
        return  $newArray;
    }
}
